package part3.c13_active_object;

/**
 * Created by lie on 2018/3/23.
 * 展现多线程效果
 * 实际并不复杂
 *      我们注意到每一个加进engine里的command都会被执行，并且执行完毕以后，才会到下一个
 *      同时，每一个{@link SleepCommand}实例，都维护着它的一个执行"序列"——sleep完以后执行构造里的另一个command
 */
public class MultiDelayCommand2 implements Command {

    public static ActiveObjectEngine sEngine = new ActiveObjectEngine();
    public static boolean isStop = false;

    private char aChar;
    private long delay;

    public MultiDelayCommand2(long delay, char character) {
        this.delay = delay;
        this.aChar = character;
    }

    public void execute() throws Exception {
        System.out.print(aChar);

        if (!isStop) {
//            sEngine.addCommand(this);
            delayAndRepeat();
        }

    }

    private void delayAndRepeat() {
        //加进了sleepCommand，所以因为延时效果（而且cpu时钟和实时时钟不是完美同步的），所以有多线程效果
        sEngine.addCommand(new SleepCommand(this.delay, sEngine, this));
    }


}
